#!/bin/sh
"""": # -*-python-*-
bup_exec="$(dirname "$0")/bup-exec" || exit $?
exec "$bup_exec" "$0" ${1+"$@"}
"""

from __future__ import absolute_import, print_function

import grp
import pwd
import sys

from bup.compat import get_argv, get_argvb

def usage():
    print('Usage: id-other-than <--user|--group> ID [ID ...]',
          file=sys.stderr)

argvb = get_argvb()

if len(argvb) < 2:
    usage()
    sys.exit(1)

def is_integer(x):
    try:
        int(x)
        return True
    except ValueError as e:
        return False

excluded_ids = set(int(x) for x in argvb[2:] if is_integer(x))
excluded_names = (x for x in get_argv()[2:] if not is_integer(x))

if argvb[1] == b'--user':
    for x in excluded_names:
        excluded_ids.add(pwd.getpwnam(x).pw_uid)
    for x in pwd.getpwall():
        if x.pw_uid not in excluded_ids:
            print(x.pw_name + ':' + str(x.pw_uid))
            sys.exit(0)
elif argvb[1] == b'--group':
    for x in excluded_names:
        excluded_ids.add(grp.getgrnam(x).gr_gid)
    for x in grp.getgrall():
        if x.gr_gid not in excluded_ids:
            print(x.gr_name + ':' + str(x.gr_gid))
            sys.exit(0)
else:
    usage()
    sys.exit(1)
